﻿using Core.Business.ProjectModule;
using Core.Business.SocketModel;
using Core.Business.UserModule;
using Core.DataAccess.Model.Project.Queue;
using Core.DataAccess.Model.Projects;
using Core.Framework.Model.Common;
using Core.Framework.Redis;
using Core.IBusiness.IProjectModule;
using Core.IBusiness.ISocketModel;
using Core.IBusiness.IUserModule;
using Core.Middleware.WebSockets;
using log4net.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace Core.WebSocketApi
{
    public class Startup
    {
        public static ILoggerRepository repository { get; set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            CoreStartupHelper.Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //services
            //    .AddDbContext<ProjectContext>();

            

            services
                 .AddMvc()
                 .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            BuildServiceProvider
                .serviceCollection

                .AddScoped<ProjectContext>()
                .AddScoped<ProjectSocketContext>()

                .AddSingleton<IProject, Project>()
                .AddSingleton<IProjectUser, ProjectUsers>()

                .AddSingleton<ISocketGroup, SocketGroup>()
                .AddSingleton<ISocketRelationship, SocketRelationship>()
                .AddSingleton<ISocketMessage, SocketMessage>()
                
                .AddSingleton<ISocketUser,SocketUsers>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseWebSockets(new WebSocketOptions()
            {
                KeepAliveInterval = TimeSpan.FromSeconds(10)
            });

            app
                .UseWebSocketsMiddleware()
                .UseQueueLoop()
                .UseQueueUntreatedLoop();

            app.UseCors(builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());

            app.UseMvc();

        }
    }
}
